package io.renren.modules.knowledge.controller;

import io.renren.common.utils.R;
import io.renren.modules.course.entity.TreeEntity;
import io.renren.modules.knowledge.entity.Edge;
import io.renren.modules.knowledge.entity.KnowledgeEntity;
import io.renren.modules.knowledge.entity.Node;
import io.renren.modules.knowledge.entity.node.UseNeo;
import io.renren.modules.knowledge.entity.node.course;
import io.renren.modules.knowledge.entity.node.knowledge;
import io.renren.modules.knowledge.entity.node.occupation;
import io.renren.modules.knowledge.entity.relationship.IdEntity;
import io.renren.modules.knowledge.entity.ship.MasterShip;
import io.renren.modules.knowledge.respository.CourseRespository;
import io.renren.modules.knowledge.respository.KnowledgeRespository;
import io.renren.modules.knowledge.respository.MasterShipRespository;
import io.renren.modules.knowledge.respository.OccupationRespository;
import io.renren.modules.knowledge.service.KnowledgeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("knowledge")
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;
    @Autowired
    private OccupationRespository occupationRespository;
    @Autowired
    private CourseRespository courseRespository;
    @Autowired
    private MasterShipRespository masterShipRespository;
    @Autowired
    private KnowledgeRespository knowledgeRespository;



    /**
     * 增加课程间关系
     */
    @RequestMapping("/addRelatrionShip")
    public R addRelatrionShip(@RequestBody MasterShip c){
       // courseRespository.save(c);
        Long occId = c.getOccupation().id;
        if(null == occId || "".equals(occId)){
            return R.error("数据错误");
        }
        Long courseId = c.getCourse().id;
        masterShipRespository.saveOccAndCourse(occId,courseId);
        //List<KnowledgeEntity> direction = knowledgeService.getDirection(params);
        return R.ok();
    }
    /**
     * 增加课程
     */
    @RequestMapping("/addCourse")
    public R AddCourse(@RequestBody course c){
        if(null !=c.getName()){
            courseRespository.save(c);
        }else {
            return R.error(102,"课程名称不能为空");
        }
        //List<KnowledgeEntity> direction = knowledgeService.getDirection(params);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/getDirection")
    public R getDirection(@RequestBody Map<String, Object> params){
        List<KnowledgeEntity> direction = knowledgeService.getDirection(params);
        return R.ok().put("direction",direction);
    }

    /**
     * 课程列表
     */
    @RequestMapping("/getAllCourse")
    public R getAllCourse(String search){
        Object courses;
        List<TreeEntity> list =new ArrayList();

        courses=  courseRespository.findAll();
        if("".equals(search)){
            for(course c:(List<course>)courses){
                TreeEntity t=new TreeEntity();
                t.setId(c.id);
                t.setName(c.getName());
                t.setParentId(new Long(-1));
                list.add(t);
            }
        }
        else {
            for(course c:(List<course>)courses){
                if(null != c.getName()){
                    if(c.getName().contains(search)) {
                        TreeEntity t = new TreeEntity();
                        t.setId(c.id);
                        t.setName(c.getName());
                        t.setParentId(new Long(-1));
                        list.add(t);
                    }
                }
            }
        }


        return R.ok().put("tree",list);
    }


    /**
     * 列表
     */
    @RequestMapping("/getCourseList")
    public R getCourseList(@RequestBody Map<String, Object> params){
       // Optional<occupation> o=occupationRespository.findById(new Long((String) params.get("occupation")),2);
        Long id =Long.valueOf(params.get("occupation").toString());
        Optional<occupation> o=occupationRespository.findById(id,2);
        Set<course> courses=  o.get().getCourse();
        List<Edge> edges=new ArrayList<>();
        List<Node> nodes=new ArrayList<>();
        for(course c:courses){
            Node node=new Node();
            node.setId(c.id);
            node.setLabel(c.getName());
            nodes.add(node);
            if(c.getCourse()!=null) {
                for (course c1 : c.getCourse()) {
                    Edge edge = new Edge();
                    edge.setFrom(c.id);
                    edge.setTo(c1.id);
                    edges.add(edge);
                }
            }
        }

        return R.ok().put("course",o.get().getCourse()).put("nodes",nodes).put("edges",edges);
    }


    /**
     * 删除课程并删除该课程与职业间的关系
     */

    /**
     * 增加课程间关系
     */
    @PostMapping("saveCoursesRelactionShip")
    public R saveCoursesRelactionShip(@RequestBody IdEntity idEntity){

        if(null == idEntity.getStartId() || null == idEntity.getEndId()){
            return R.error(101,"请选择开始或结束课程");
        }

        Long startId =idEntity.getStartId();
        Long endId = idEntity.getEndId();

        if(startId == endId  || startId.equals(endId) || startId.toString().equals(endId.toString())){
            return R.error(101,"");
        }

        knowledgeService.saveCoursesRelactionShip(startId,endId);

        return R.ok();
    }


    /**
     * 删除课程间关系
     */
    @PostMapping("delCourseAndShip")
    public R delCourseAndShip(@RequestBody  IdEntity idEntity){
        Long deleteId = idEntity.getEndId();
        if(null == deleteId){
            return R.error("请选择要删除的课程");
        }

        knowledgeService.delCourseAndShip(deleteId);

        return R.ok();
    }


    /**
     * 删除课程间关系 直接传id  不适用node形式
     */
    @PostMapping("delCourseAndShipNoNodes")
    public R delCourseAndShipNoNodes(@RequestBody IdEntity idEntity){
        if(null == idEntity.getEndId()){
            return R.error("请选择要删除的课程");
        }

        Long deleteId = Long.valueOf(idEntity.getEndId());
        knowledgeService.delCourseAndShipNoNodes(deleteId);

        return R.ok();
    }



    /**
     * 知识点列表
     */
    @RequestMapping("/getAllKnowledge")
    public R getAllKnowledge(String search){
        Object knowledges;
        List<TreeEntity> list =new ArrayList();

        knowledges=  knowledgeRespository.findAll();
        if("".equals(search)){
            for(knowledge c:(List<knowledge>)knowledges){
                TreeEntity t=new TreeEntity();
                t.setId(c.id);
                t.setName(c.getName());
                t.setParentId(new Long(-1));
                list.add(t);
            }
        }
        else {
            for(knowledge c:(List<knowledge>)knowledges){
                if(c.getName().contains(search)) {
                    TreeEntity t = new TreeEntity();
                    t.setId(c.id);
                    t.setName(c.getName());
                    t.setParentId(new Long(-1));
                    list.add(t);
                }
            }
        }


        return R.ok().put("tree",list);
    }


    /**
     * 增加知识点
     */
    @RequestMapping("/addKnowledgePoint")
    public R addKnowledgePoint(@RequestBody knowledge k){
        if(null !=k.getName()){
            knowledgeRespository.save(k);
        }else {
            return R.error(102,"知识点名称不能为空");
        }
        //List<KnowledgeEntity> direction = knowledgeService.getDirection(params);
        return R.ok();
    }


    /**
     * 同一课程下增加知识点关系
     */
    @PostMapping("addKnowledgePointRelationship")
    public R addKnowledgePointRelationship(@RequestBody IdEntity idEntity ){

        if(idEntity.getStartId()==idEntity.getEndId()){
            return R.error(101,"不能相同节点创建关系哦");
        }

        knowledgeRespository.addKnowledgePointRelationship(idEntity.getStartId(),idEntity.getEndId());
        return R.ok();
    }

    /**
     * 非同一课程下增加知识点关系
     */
    @PostMapping("addNoOneKnowledgePointRelationship")
    public R addNoOneKnowledgePointRelationship(@RequestBody IdEntity idEntity ){
        if(idEntity.getStartId()==idEntity.getEndId()){
            return R.error(102,"不能相同节点创建关系哦");
        }
        knowledgeRespository.addNoOneKnowledgePointRelationship(idEntity.getStartId(),idEntity.getEndId());
        return R.ok();
    }

    /**
     * 添加课程与知识点的关系
     */
    @PostMapping("saveKnowledgeInCourseRelationship")
    public R saveKnowledgeInCourseRelationship(@RequestBody IdEntity idEntity){

        if(idEntity.getStartId()==idEntity.getEndId()){
            return R.error(102,"不能相同节点创建关系哦");
        }

        Long courseId = idEntity.getStartId();
        Long knowledgeId = idEntity.getEndId();

        knowledgeRespository.saveKnowledgeInCourseRelationship(courseId,knowledgeId);

        return R.ok();

    }



    /**
     * 删除知识点及关系
     */
    @PostMapping("delKnowledgeAndRelationShip")
    public R delKnowledgeAndRelationShip(@RequestBody IdEntity idEntity ){
        if(null == idEntity.getStartId()){
            return R.error(102,"请选择要删除的知识点");
        } else{
            knowledgeRespository.delKnowledgeAndRelationShip(idEntity.getStartId());
            return R.ok();
        }
    }


    @PostMapping("getKnowledgeList")
    public R getKnowledgeList(@RequestBody IdEntity idEntity){
        Long id = idEntity.getStartId();

        /*Optional<course> courses = courseRespository.findById(id,2);
        Set<course> courses=  occupation.get().getCourse();*/
        Optional<course> coursess = courseRespository.findById(id,2);
        List<knowledge> knowledges = coursess.get().getKnowledge();
        List<Edge> edges=new ArrayList<>();
        List<Node> nodes=new ArrayList<>();
        /*for(course c :courses){
            if(null != c.getKnowledge()){
                //获取课程下的包含关系的知识点
                List<knowledge> knowledges =  c.getKnowledge();*/

                //根据知识点的id找其下一层
                for(knowledge knowledge :knowledges){
                    Node node = new Node();
                    node.setId(knowledge.id);
                    node.setLabel(knowledge.getName());
                    nodes.add(node);
                    if(null != knowledge.getDownKnowledge() ){  //第二层的知识点
                        for(knowledge twoKnowledge:knowledge.getDownKnowledge()){
                            Edge edge = new Edge();
                            edge.setFrom(knowledge.id);
                            edge.setTo(twoKnowledge.id);
                            edge.setLabel(twoKnowledge.getName());
                            edges.add(edge);
                        }
                    }


                }
         /*   }
        }*/
return R.ok().put("nodes",nodes).put("edges",edges);
    }

}
